/**
 * 
 */
package info.niwota.profile.contact;

import java.util.Set;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

/**
 * 
 * @author qiangli
 * 
 */
// cd /data/data/com.android.providers.contacts/databases
// sqlite3 contacts.db
public abstract class ContactAdapter {
	private static ContactAdapter adapter;

	//private static final int VERSION_CODES_ECLAIR = 5;

	static final String TAG = "ContactAdapter";

	public static ContactAdapter getAdapter() {
		if (adapter == null) {
			try {

				adapter =  new ContactAdapter2();
				
				//Log.d(TAG, "class: " + adapter.getClass().getName() +" adapter: " + adapter);
			} catch (Exception e) {
				throw new IllegalStateException(e);
			}
		}
		return adapter;
	}

	/**
	 * Load contact and group, shadow/ignore entries in group with identical names
	 *
	 * @param context
	 * @param contacts
	 */
	public void loadEmailContacts(Context ctx, Set<EmailContact> contacts) {
			//load from contact first and shadow entries in group with same name (if any)
			loadContactsInConact(ctx, contacts);
			//group contact
			//GroupContacts.loadGroups(ctx, contacts);
	}

	protected abstract void loadContactsInConact(Context ctx, Set<EmailContact> contacts);

	/**
	 * Load aggregated list of addresses from both group and contact
	 * 
	 * @param context
	 * @param addresses
	 */
	public void loadEmailAddresses(Context ctx, Set<EmailAddress> addresses) {
		//load from contact first as it may have a better name
		loadAddressesInConact(ctx, addresses);
		//load from group
		//GroupContacts.loadAddresses(ctx, addresses);
	}
	
	protected abstract void loadAddressesInConact(Context ctx, Set<EmailAddress> addresses);

	/**
	 * Load group or contact photo
	 * @param context
	 * @param id
	 * @param res
	 * @return
	 */
	public Bitmap loadPhoto(Context ctx, long id, int res) {
		if (id == -1) {
			return BitmapFactory.decodeResource(ctx.getResources(), res);
		}
		if (isGroup(id)) {
			return loadGroupPhoto(ctx, id, res);
		} else {
			return loadContactPhoto(ctx, id, res);
		}
	}
	
	protected abstract Bitmap loadContactPhoto(Context ctx, long id, int res);

	private Bitmap loadGroupPhoto(Context ctx, long id, int res) {
		//TODO  group photo
		return BitmapFactory.decodeResource(ctx.getResources(), res);
	}

	private boolean isGroup(long id) {
		return (id < -1);
	}

	/**
	 * Get email from contact first, then from group if name is a group name and it is not found in contact
	 * @param context
	 * @param name
	 * @return
	 */
	public String[] getEmails(Context ctx, String name) {
		if (name == null) {
			return null;
		}
		String[] emails = getEmailsInContact(ctx, name);
		
		return emails;
	}
	
	protected abstract String[] getEmailsInContact(Context ctx, String name);

	
	public abstract Intent getIntent();

	public static Uri buildContactUri(long contactId) {
		Uri uri = ContactsContract.RawContacts.CONTENT_URI;
		return uri.buildUpon().appendPath(contactId + "").build();
	}
}
